define(["olesk/FSM", "doh"], function (FSM, doh) {
    
    doh.register("fwrdTest", [
        {
            name: "State is incremented by 1(one).",
            setUp: function () {
                this.carusel = new FSM.LinearFSM({
                    len: 10,
                    transition_handler: function (from, to) {
                        var promise = new dojo.Deferred();
                        setTimeout(function () {
                                       promise.resolve({from:from, to:to});
                                   }, 100);
                        return promise;
                    }         
                });
            },
            runTest: function () {
                var carusel = this.carusel;

                carusel.fwrd();
                
                doh.assertEqual(0, this.carusel.state);
                
                var deferred = new doh.Deferred();
                setTimeout(deferred.getTestCallback(function () {
                    doh.assertEqual(1, carusel.state);
                }), 200);
                return deferred;
            },
            tearDown: function () {
                delete this.carusel;
            },
            timeout: 300 // 3 second timeout.
        },
        {
            name: "State is unchanged if FSM is in the end state.",
            setUp: function () {
                this.carusel = new FSM.LinearFSM({
                    len: 10,
                    transition_handler: function (from, to) {
                        var promise = new dojo.Deferred();
                        setTimeout(function () {
                                       promise.resolve({from:from, to:to});
                                   }, 100);
                        return promise;
                    }         
                });
                // setting state to end state
                this.carusel.state = 9;
            },
            runTest: function () {
                var carusel = this.carusel;

                carusel.fwrd();
                
                doh.assertEqual(9, this.carusel.state);
                
                var deferred = new doh.Deferred();
                setTimeout(deferred.getTestCallback(function () {
                    doh.assertEqual(9, carusel.state);
                }), 200);
                return deferred;
            },
            tearDown: function () {
                delete this.carusel;
            },
            timeout: 300 // 3 second timeout.
        },
        {
            name: "Commands are ignored if FSM is in transition.",
            setUp: function () {
                this.carusel = new FSM.LinearFSM({
                    len: 10,
                    transition_handler: function (from, to) {
                        var promise = new dojo.Deferred();
                        setTimeout(function () {
                                       promise.resolve({from:from, to:to});
                                   }, 100);
                        return promise;
                    }         
                });
            },
            runTest: function () {
                var carusel = this.carusel;

                carusel.fwrd();
                // should be ignored
                carusel.fwrd();
                
                var deferred = new doh.Deferred();
                setTimeout(deferred.getTestCallback(function () {
                    doh.assertEqual(1, carusel.state);
                }), 200);
                return deferred;
            },
            tearDown: function () {
                delete this.carusel;
            },
            timeout: 300 // 3 second timeout.
        }
    ]);

    doh.register("bwrdTest", [
        {
            name: "State is decremented by 1.",
            setUp: function () {
                this.carusel = new FSM.LinearFSM({
                    len: 10,
                    transition_handler: function (from, to) {
                        var promise = new dojo.Deferred();
                        setTimeout(function () {
                                       promise.resolve({from:from, to:to});
                                   }, 100);
                        return promise;
                    }         
                });
                // forcing state 3
                this.carusel.state = 3;
            },
            runTest: function () {
                var carusel = this.carusel;

                carusel.bwrd();
                
                doh.assertEqual(3, this.carusel.state);
                
                var deferred = new doh.Deferred();
                setTimeout(deferred.getTestCallback(function () {
                    doh.assertEqual(2, carusel.state);
                }), 200);
                return deferred;
            },
            tearDown: function () {
                delete this.carusel;
            },
            timeout: 300 // 3 second timeout.
        },
        {
            name: "State is unchanged if FSM is in the start state.",
            setUp: function () {
                this.carusel = new FSM.LinearFSM({
                    len: 10,
                    transition_handler: function (from, to) {
                        var promise = new dojo.Deferred();
                        setTimeout(function () {
                                       promise.resolve({from:from, to:to});
                                   }, 100);
                        return promise;
                    }         
                });
            },
            runTest: function () {
                var carusel = this.carusel;

                carusel.bwrd();
                
                doh.assertEqual(0, this.carusel.state);
                
                var deferred = new doh.Deferred();
                setTimeout(deferred.getTestCallback(function () {
                    doh.assertEqual(0, carusel.state);
                }), 200);
                return deferred;
            },
            tearDown: function () {
                delete this.carusel;
            },
            timeout: 300 // 3 second timeout.
        },
        {
            name: "Commands are ignored if FSM is in transition.",
            setUp: function () {
                this.carusel = new FSM.LinearFSM({
                    len: 10,
                    transition_handler: function (from, to) {
                        var promise = new dojo.Deferred();
                        setTimeout(function () {
                                       promise.resolve({from:from, to:to});
                                   }, 100);
                        return promise;
                    }         
                });
                // forcing state 3
                this.carusel.state = 3;
            },
            runTest: function () {
                var carusel = this.carusel;

                carusel.bwrd();
                // should be ignored
                carusel.bwrd();
                
                var deferred = new doh.Deferred();
                setTimeout(deferred.getTestCallback(function () {
                    doh.assertEqual(2, carusel.state);
                }), 200);
                return deferred;
            },
            tearDown: function () {
                delete this.carusel;
            },
            timeout: 300 // 3 second timeout.
        }
    ]);


});

